const { log, e, appendHtml } = require('../helper')
// 引入 Node 内置方法 path.basename() 从文件路径获得文件名
const path = require('path')
const { ipcRenderer } = require('electron')

let music_files_path = []
e('#id-button-choose').addEventListener('click', (event) => {
    ipcRenderer.send('open-choose-music')
})

e('#id-button-import').addEventListener('click', (event) => {
    ipcRenderer.send('import-music', music_files_path)
    window.close()
})

const renderMusicList = (paths) => {
    let div = e('#id-music-list')
    // 用 map 处理 paths，并用 appendHTML 函数将元素插入 DOM
    paths.map((e) => {
        let name = path.basename(e)
        let li = `<li class="list-group-item">${name}</li>`
        appendHtml(div, li)
    })
}

ipcRenderer.on('choosed-music-filePaths', (event, paths) => {
    if(Array.isArray(paths)) {
        // let music_paths = paths
        renderMusicList(paths)
        music_files_path = paths
    } else {log('arg 错误')}
})

